<template>
  <div>
    <el-form ref="deptForm" label-width="120px">
      <el-form-item label="部门名称">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in List" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
import { getEmployee } from '@/api/employee'
import { addDepartment } from '@/api/departments'
export default {
  props: {
    id: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      List: [] // 存储员工列表
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    // 确定
    hSubmit() {
      this.doAdd()
    },
    // 取消
    hCancel() {},
    // 获取员工列表
    async  loadEmployee() {
      try {
        const res = await getEmployee()
        this.List = res.data
        console.log(res.data)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门
    async doAdd() {
      try {
        // 将父级传过来的id作为自己的pid
        const res = await addDepartment({ pid: this.id, ...this.form })
        console.log('res', res)
        // this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
